<script>
	import mqtt from 'mqtt/dist/mqtt.min.js';
	export default {
		globalData: {
			mqttClient: null,
			loginTempToken: '',
			marshalling_id: 334,
			company_id: 173, //企业id
			version: '4.3.5',
			cabinet_id: 'wt2506',
			id:'',
			useOldReplaceNew: false, //是否使用以旧换新
			deviceId:'',
			is_admin:false,
		},
		onLaunch: function() {
			console.log('App Launch')
		},
		onShow: function() {
			console.log('App Show')
		},
		onHide: function() {
			console.log('App Hide')
		},
		methods: {
			async connect() {
				
				const options = {
					clean: true, // true: 清除会话, false: 保留会话
					connectTimeout: 4000, // 超时时间
					// // 认证信息
					clientId: 'emqx_test' + Math.random(),
					username: 'admin',
					password: 'Wate2020.',
					deviceId:''
				};
				this.globalData.id = options.clientId;
				// #ifdef H5
				this.globalData.mqttClient = mqtt.connect('ws://test.mosquitto.org:8080')
				// #endif
				// #ifdef MP-WEIXIN||APP-PLUS
				this.globalData.mqttClient = mqtt.connect('wx://m.iwate.cn:8083/mqtt', options)
				// this.globalData.mqttClient = mqtt.connect('wxs://m.iwate.cn:8084/mqtt', options)
		
				// #endif
				const client = this.globalData.mqttClient; // Store reference to the client
				client.on('connect', function() {
					console.log('connect');
					client.subscribe('ZHZN/ZHZN002', function(err) {
						if (!err) {
							console.log('订阅成功');
						}
					})
				}).on('reconnect', function() {
		
					console.log("on reconnect");
				}).on('error', function() {
					console.log('on error');
		
				}).on('end', function() {
					console.log('on end');
		
				}).on('message', function(topic, message) {
					uni.$emit('messageMqtt', topic, message);
				})
			}
		}
	}
</script>

<style lang="scss">
	/*每个页面公共css */
	@import '@/uni_modules/uni-scss/index.scss';
	/* #ifndef APP-NVUE */
	@import '@/static/customicons.css';
	// 设置整个项目的背景色
	page {
		background-color: #f5f5f5;
	}

	/* #endif */
	.example-info {
		font-size: 14px;
		color: #333;
		padding: 10px;
	}
</style>
